<?php
/**
 * Created by IntelliJ IDEA.
 * User: xingshi
 * Date: 16/9/1
 * Time: 下午2:34
 */

namespace App\Console\Commands;

use App\Utils;
use Illuminate\Console\Command;
use DB;
use App\User;
use App\Company;
use App\Job;
use App\Techlead;
use Auth;

//use Illuminate\Foundation\Inspiring;

class Insert2 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'insert2';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'insert2 data';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        var_dump("begin:");

        $fi = file("/root/work/app/Console/Commands/nt.txt");
	$com_cnt=0;
        foreach($fi as $k=>$line){
            $line = rtrim($line);
            $obj = json_decode($line);

            $com_ret = self::com($obj);
			if($com_ret["companyid"] == "no") continue;
			else if($com_ret["companyid"] == "") {
				$company = Company::create($com_ret["com_mp"]);
			}

		var_dump("123");
	$com_cnt+=1;
        }

        var_dump("end".$com_cnt);
    }

    private function com($obj){
		if($obj->name==NULL || $obj->domain==NULL||$obj->size==NULL||$obj->logoUrl==NULL||$obj->foundation==NULL||$obj->location==NULL||$obj->industry==NULL||$obj->slogan==NULL||$obj->introduction==NULL) 
            return array("companyid"=> "no", "com_mp"=>"");

        $com_mp = array();
        $com_mp["name"] = $obj->name;
        $com_mp["domain"] = $obj->domain;
        $com_mp["size"] = $obj->size;
        $com_mp["avatar_url"] = $obj->logoUrl;
        $com_mp["foundation"] = $obj->foundation;
        $com_mp["location"] = $obj->location;
        $com_mp["industry"] = $obj->industry;
        $com_mp["slogan"] = $obj->slogan;
        $com_mp["introduction"] = $obj->introduction;
        $com_mp["tags"] = json_encode($obj->tags, JSON_UNESCAPED_UNICODE);
        $com_mp["isreal"] = 0;
        $com_mp["isdelete"] = 0;
        $data = Company::whereRaw("name = ?", [$obj->name])->get();

        if(count($data)>0){
            return array("companyid"=> $data[0]->id, "com_mp"=>$com_mp);
        }
        else{
            return array("companyid"=> "", "com_mp"=>$com_mp);
        }
    }
}
